/* 
 * File:   JClientStub.cpp
 * Author: johnneyshi
 * 
 * Created on 2013年5月14日, 下午8:10
 */

#include "JClientStub.h"
#include "JSerialize.h"
#include "JStubArgument.h"
#include "JInOutPutFstream.h"
#include "JString.h"
#include "JRepositoryConfig.h"
#include <iostream>

JClientStub::JClientStub() 
{
    //初始化JInOutPut*
    pOutStrea = new JInOutPutFstream(JRepositoryConfig::clientOutTempStreamFileName);
    pInStrea = new JInOutPutFstream(JRepositoryConfig::clientInTempStreamFileName);
    //((JInOutPutFstream*)pStrea)->OpenStream(std::ios::binary | std::ios::trunc | std::ios::out);
}

JClientStub::~JClientStub() 
{
    //((JInOutPutFstream*)pStrea)->CloseStream();
}

JCommitConflict JClientStub::Commit(JWorkingCopies& _workingCopies,JWorkingCopiesBaseFile& _copyBaseFile,JBaseFile& _baseFile)
{
    StubHead();
    int classSize = 0,selfSize = 0;
    std::list<std::string> listArg;
    listArg.push_back("JWorkingCopies&");
    listArg.push_back("JWorkingCopiesBaseFile&");
    listArg.push_back("JBaseFile&");
    JStubArgument::PackStubMessageHead(*pOutStrea,selfSize,classSize,"Commit",listArg);
    _workingCopies.PackStubMessage(*pOutStrea,classSize);
    _copyBaseFile.PackStubMessage(*pOutStrea,classSize);
    _baseFile.PackStubMessage(*pOutStrea,classSize);
    JStubArgument::PackStubMessageTail(*pOutStrea,selfSize,classSize);
    JCommitConflict commandConflict;
    StubTail(commandConflict);
    return commandConflict;
}

void JClientStub::StubHead()
{
    ((JInOutPutFstream*)pOutStrea)->OpenStream(std::ios::binary | std::ios::trunc | std::ios::out);
}

void JClientStub::StubTail(JStubArgument& _returnValue)
{
    ((JInOutPutFstream*)pOutStrea)->CloseStream();
    SendDataToClient();
    GetDataFromClient();
    ((JInOutPutFstream*)pInStrea)->OpenStream(std::ios::binary | std::ios::in);
    _returnValue.UnPackStubMessage(*pInStrea);
    ((JInOutPutFstream*)pInStrea)->CloseStream();
}

JUpdateConflict JClientStub::Update(JWorkingCopies& _workingCopies,JWorkingCopiesBaseFile& _copyBaseFile,JBaseFile& _baseFile)
{
}

JRepositoryOperationResult JClientStub::GetLock(JWorkingCopies& _workingCopies,JString& _lockType)
{
    StubHead();
    int classSize = 0,selfSize = 0;
    std::list<std::string> listArg;
    listArg.push_back("JWorkingCopies&");
    listArg.push_back("JString&");
    JStubArgument::PackStubMessageHead(*pOutStrea,selfSize,classSize,"GetLock",listArg);
    _workingCopies.PackStubMessage(*pOutStrea,classSize);
    _lockType.PackStubMessage(*pOutStrea,classSize);
    JStubArgument::PackStubMessageTail(*pOutStrea,selfSize,classSize);
    JRepositoryOperationResult result;
    StubTail(result);
    return result;
}

JRepositoryOperationResult JClientStub::ReleaseLock(JWorkingCopies& _workingCopies)
{
    StubHead();
    int classSize = 0,selfSize = 0;
    std::list<std::string> listArg;
    listArg.push_back("JWorkingCopies&");
    JStubArgument::PackStubMessageHead(*pOutStrea,selfSize,classSize,"ReleaseLock",listArg);
    _workingCopies.PackStubMessage(*pOutStrea,classSize);
    JStubArgument::PackStubMessageTail(*pOutStrea,selfSize,classSize);
    JRepositoryOperationResult result;
    StubTail(result);
    return result;
}

void JClientStub::SendDataToClient()
{
    clientSVN.Send();
}

void JClientStub::GetDataFromClient()
{
    clientSVN.Recv();
}